home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 2010 April
/
PCWorld0410.iso
/
pluginy Firefox
/
14642
/
14642.xpi
/
chrome
/
content
/
overlay.js
< prev
next >
Wrap
Text File
|
2009-10-14
|
11KB
|
325 lines
/* Copyright 2009, Boomtango.com. All Rights Reserved. */
/* overlay.js
* Responsible for initializing app
*/
Components.utils.import("resource://boomtango/app.js");
window.addEventListener("load", function() { boomtangoOverlay.init(document); }, false);
var boomtangoOverlay = {
cc: Components.classes,
ci: Components.interfaces,
init: function() {
this.app = boomtangoApp;
this.app.init();
if(this.app.newUser){
this.doNewUser();
this.app.newUser = false;
}
this.browser = document.getElementById("content");
gBrowser.tabContainer.addEventListener(
"TabSelect",
function(e){
boomtangoOverlay.onTabChange(e);
},
false
);
gBrowser.tabContainer.addEventListener(
"TabOpen",
function(e){
boomtangoOverlay.onTabOpen(e);
},
false
);
var appcontent = document.getElementById("appcontent"); // browser
if(appcontent){
appcontent.addEventListener("DOMContentLoaded", function(e){
boomtangoOverlay.onPageLoad(e);
}, true);
}
this.initPalette();
this.initGeolocation();
this.app.firsttime = false;
},
initGeolocation: function(){
/*
if(Components.classes["@mozilla.org/geolocation/provider;1"]){
var callback = {
Update: function(pos){
dump("Coords: " + pos.coords.latitude + ", " + pos.coords.longitude + "\n");
},
};
var geoprovider =
Cc["@mozilla.org/geolocation/provider;1"].getService(Ci.nsIGeolocationProvider);
geoprovider.startup();
geoprovider.watch(callback);
}
*/
},
initPalette: function() {
try {
var firefoxnav = document.getElementById("nav-bar");
var curSet = firefoxnav.currentSet;
if (curSet.indexOf("viewBoomtangoButtonIcon") == -1 && this.app.firsttime)
{
var set;
// Place the button before the urlbar
if (curSet.indexOf("reload-button") != -1)
set = curSet.replace(/reload-button/, "viewBoomtangoButtonIcon,reload-button");
else // at the end
set = curSet + ",viewBoomtangoButtonIcon";
firefoxnav.setAttribute("currentset", set);
firefoxnav.currentSet = set;
document.persist("nav-bar", "currentset");
// If you don't do the following call, funny things happen
try {
BrowserToolboxCustomizeDone(true);
}
catch (e) { }
}
} catch(e) { }
// if mac, change icon
if (navigator.platform.toLowerCase().indexOf('mac') > -1){
var button = document.getElementById("viewBoomtangoButtonIcon");
if(button){
button.className += " macosup";
}
}
},
forEachTab: function(func, funcThis){
var b = this.browser;
var len = b.browsers.length;
while(len--){
var curr = b.getBrowserAtIndex(len);
if(!curr.hasAttribute("boomtangoID")){
curr.setAttribute("boomtangoID",boomtangoApp.createTabID());
}
if(func.apply(this, [curr, curr.currentURI.spec])){
return;
}
}
},
test: function(){
var panel = document.getElementById("bttest");
panel.openPopup(
document.getElementById("urlbar"), "after_end", 0, 0, false, false);
},
generateThumb: function(doc){
var win = doc.defaultView;
var canvas = document.getElementById("boomtangoThumbGen");
return boomtangoApp.generateThumb(win, canvas);
},
ONEWEEK: 7 * 24 * 60 * 60 * 1000,
_initialOnLoadHasBeenCalled: false,
_currentLoadedURL: null,
onTabOpen: function(e){
boomtangoApp.onTabOpen(e.target.linkedBrowser);
},
_tabChangeTimer: null,
onTabChange: function(e){
// short circuit this if in private browsing mode
var pbcls = Components.classes["@mozilla.org/privatebrowsing;1"];
if(pbcls){
var pbs = pbcls.getService(
Components.interfaces.nsIPrivateBrowsingService);
if(pbs.privateBrowsingEnabled){
return;
}
}
if(this._initialOnLoadHasBeenCalled){
var self = this;
if(self._tabChangeTimer){
self._tabChangeTimer.cancel();
self._tabChangeTimer = null;
}
var callback = {
browserID: gBrowser.selectedTab.linkedBrowser.getAttribute("boomtangoID"),
notify: function(){
if(this.browserID == gBrowser.selectedTab.linkedBrowser.getAttribute("boomtangoID")){
boomtangoApp.onTabChange(gBrowser.selectedTab.linkedBrowser);
if(self._currID){
self.handlePageUnload(self._currID, self._currDoc);
}
if(boomtangoApp.inBlacklist(gBrowser.selectedTab.linkedBrowser.contentDocument.location.href)){
return;
}
self.handlePageLoad(gBrowser.selectedTab.linkedBrowser.contentDocument);
}
if(self._tabChangeTimer){
self._tabChangeTimer.cancel();
self._tabChangeTimer = null;
}
}
};
self._tabChangeTimer = Components.classes["@mozilla.org/timer;1"].
createInstance(Components.interfaces.nsITimer);
self._tabChangeTimer.initWithCallback(
callback,
this.app.tabChangeTime * 1000,
self._tabChangeTimer.TYPE_ONE_SHOT
);
}
},
handlePageUnload: function(id, doc){
if(this._newthumbtimer){
this._newthumbtimer.cancel();
this._newthumbtimer = null;
}
if(this._newthumb && doc && doc.location){
var thumbrec = boomtangoApp.getThumbID(doc.location.href);
var d =this.generateThumb(doc);
boomtangoApp.updateThumb(thumbrec.id, d);
}
boomtangoApp.onPageUnload(id,doc);
this._currDoc = null;
this._currID = null;
this._newthumb = false;
},
handlePageLoad: function(doc){
// short circuit this if in private browsing mode
var pbcls = Components.classes["@mozilla.org/privatebrowsing;1"];
if(pbcls){
var pbs = pbcls.getService(
Components.interfaces.nsIPrivateBrowsingService);
if(pbs.privateBrowsingEnabled){
return;
}
}
if(this._newthumbtimer){
this._newthumbtimer.cancel();
this._newthumbtimer = null;
}
var id = gBrowser.selectedBrowser.getAttribute("boomtangoID");
var url = doc.location.href;
var protocol = url.split(':')[0];
var thumb;
var newthumb = false;
var thumbrec = boomtangoApp.getThumbID(url);
var now = Date.now();
if(thumbrec.id < 0){
thumb =this.generateThumb(doc);
newthumb = true;
} else if(boomtangoApp.refreshRequired(thumbrec.createtime)) {
var d =this.generateThumb(doc);
boomtangoApp.updateThumb(thumbrec.id, d);
thumb = thumbrec.id;
newthumb = true;
} else {
thumb = thumbrec.id;
}
// time to tell the app to load it
this._currDoc = doc;
this._currID = id;
this._newthumb = newthumb;
var history = gBrowser.webNavigation.sessionHistory;
var currIndex = history.index;
var referrer = "";
if(currIndex){
var entry = history.getEntryAtIndex(currIndex - 1, false);
referrer = entry.URI.spec;
}
boomtangoApp.onPageLoad(id, doc, thumb, referrer);
var self = this;
if(newthumb){
var callback = {
notify: function(){
var thumbrec = boomtangoApp.getThumbID(doc.location.href);
var d =self.generateThumb(doc);
boomtangoApp.updateThumb(thumbrec.id, d);
self._newthumbtimer = null;
self._newthumb = newthumb;
}
};
self._newthumbtimer = Components.classes["@mozilla.org/timer;1"].
createInstance(Components.interfaces.nsITimer);
self._newthumbtimer.initWithCallback(
callback,
5 * 1000,
self._newthumbtimer.TYPE_ONE_SHOT
);
}
doc.defaultView.addEventListener(
"unload",
function(e){
if(self._currID == id){
self.handlePageUnload(id, doc);
}
},
true
);
},
onPageLoad: function(e) {
var doc = e.originalTarget;
var url = doc.location.href;
var self = this;
if(e.originalTarget.nodeName == "#document"){
var serpquery = /http:\/\/www\.google\..+\/search.*[?&]q=([^&]+)/.exec(url);
if(serpquery){
var query = serpquery[1];
boomtangoApp.doSerp(b, doc, query);
}
}
// if this is just an iframe, we don't want to know about it
if (e.originalTarget instanceof HTMLDocument &&
e.originalTarget.defaultView.frameElement) {
return;
}
// let our tab watcher know to start watching
if(!this._initialOnLoadHasBeenCalled){
var b = this.browser;
var len = b.browsers.length;
while(len--){
var curr = b.getBrowserAtIndex(len);
if(!curr.hasAttribute("boomtangoID")){
curr.setAttribute("boomtangoID",boomtangoApp.createTabID());
}
}
this._initialOnLoadHasBeenCalled = true;
}
if(boomtangoApp.inBlacklist(url)){
return;
}
if(gBrowser.selectedBrowser.contentDocument.location.href == url){
this.handlePageLoad(doc);
}
},
doNewUser: function(){
window.openDialog(
"chrome://boomtango/content/import.xul",
"Boomtango",
"chrome,centerscreen,dialog,close=yes,alwaysRaised=no"
);
},
openSettings: function() {
window.openDialog('chrome://boomtango/content/settings.xul', 'settingsDlg', 'dialog=yes,resize=no,toolbar,centerscreen').focus();
},
openHistory: function(){
this.app.openHistory(this.browser);
},
};